//
//  SZPictorialMainViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZPictorialMainViewController.h"
#import "PKRequestManager.h"
#import "CJPictorialMainTableViewCell.h"
#import "CJPictorialMainModel.h"
#import "CJPictorailImageListViewController.h"


@interface SZPictorialMainViewController () <UITableViewDataSource,UITableViewDelegate,CJPictorialMainTableViewCellDelegate>


@property (nonatomic, strong) NSMutableArray *sourceArray;  //保存请求下来的数据

@end

@implementation SZPictorialMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _sourceArray = [NSMutableArray array];  //初始化数据源数组

    
    [self requestData];   //请求数据
    [self addTableView];  //添加tableView
    
   
}


//请求数据
- (void)requestData {
    
    
    //http://morguo.com/forum.php?mod=threadgallery&androidflag=1&appfrom=ios&iosversion=3.13&page=1
    
    [PKRequestManager requestWithUrlString:@"http://morguo.com/forum.php?mod=threadgallery&androidflag=1&appfrom=ios&iosversion=3.13&page=1" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        
        NSArray *lists = dataDic[@"data"][@"list"];
        
        for (NSDictionary *dic in lists) {
            CJPictorialMainModel *model = [[CJPictorialMainModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }

        [self.pictorialTableView reloadData];   //刷新
        
    } error:^(NSError *error) {
        
    }];
    
    
}


//添加tableView
- (void)addTableView {
    
    _pictorialTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight - kNavigationBarHeight) style:(UITableViewStylePlain)];
    _pictorialTableView.dataSource = self;
    _pictorialTableView.delegate = self;
    [self.view addSubview:_pictorialTableView];
  
}


//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


//分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sourceArray.count;
}


//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *idenfitier = @"CJPictorialMainTableViewCell.h";
    
    CJPictorialMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfitier];
    if (cell == nil) {
        cell = [[CJPictorialMainTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenfitier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _sourceArray[indexPath.row];
    cell.delegate = self;
    
    return cell;
}


//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScreenWidth + 180;
}


//cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    CJPictorialMainModel *model = _sourceArray[indexPath.row];
  
    self.pictorialMainBlock(model.tid);

}






//cell的代理方法 获取点击的button
- (void)clickCell:(CJPictorialMainTableViewCell *)cell buttonTag:(NSInteger)tag {
    
    //获取cell的下表
    NSIndexPath *index = [_pictorialTableView indexPathForCell:cell];
    CJPictorialMainModel *model = _sourceArray[index.row];
    NSMutableArray *array = model.imagelist;

    CJPictorailImageListViewController *imageListVC =
    [[CJPictorailImageListViewController alloc] init];
    
    //传值图片数组
    imageListVC.imageListArray = array;
    imageListVC.numOfSize = tag;
        
    [self presentViewController:imageListVC animated:NO completion:nil];

}








@end
